local quanxue = fk.CreateSkill {

  name = "joy__quanxue",

  tags = {  },

}



quanxue:addEffect(fk.EventPhaseStart, {
  name = "joy__quanxue",
  events = {fk.EventPhaseStart,fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player == target and player:hasSkill(quanxue.name) and player.phase == Player.Play
    elseif event == fk.TurnStart then
      return target:getMark("@@joy__xue") > 0
    end
  end,
  on_cost = function (self,event, target, player, data)
    if event == fk.TurnStart then return true end
    local tos = player.room:askForChoosePlayers(player,table.map(player.room:getOtherPlayers(player),
    function(p) return p.id end),1,2,"#joy__quanxue-ask",quanxue.name,true)
    if #tos > 0 then
      event:setCostData(self, {tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local tos = event:getCostData(self)[1]
      for _, pid in ipairs(tos) do
        local p = room:getPlayerById(pid)
        if not p.dead then
          room:setPlayerMark(p,"@@joy__xue",1)
        end
      end
    else
      room:setPlayerMark(target,"@@joy__xue",0)
      local choices = {"joy__quanxue_cantuse","joy__quanxue_losehp"}
      local choice = room:askForChoice(target,choices,quanxue.name,"joy__quanxue-choice")
      if choice == "joy__quanxue_losehp" and not target.dead then
        room:loseHp(target,1,quanxue.name)
      elseif choice == "joy__quanxue_cantuse" then
        room:setPlayerMark(target,"@@joy__quanxue_cantuse-turn",1)
      end
      local targets = {}
      for _, p in ipairs(player.room:getAlivePlayers()) do
        if p:hasSkill("joy__dingli") then
          if not p.dead and not target.dead and p:hasSkill("joy__dingli") and p:getMark("joy__dingli-round") == 0 then
            local n1 = p.hp
            local n2 = target.hp
            local n = math.min(n1 - n2,2)
            if n1 <= n2 then
              if room:askForSkillInvoke(p,"joy__dingli",data,"#joy__dingli-ask1") then
                player.room:recover({
                  who = p,
                  num = 1,
                  recoverBy = p,
                  skillName = "joy__quanxue"
                  })
                p:broadcastSkillInvoke("joy__dingli")
                room:notifySkillInvoked(p, "joy__dingli")
                room:setPlayerMark(p,"joy__dingli-round",1)
              end
            elseif room:askForSkillInvoke(p,"joy__dingli",data,"#joy__dingli-ask2:::"..n) then
              p:drawCards(n,"joy__dingli")
              p:broadcastSkillInvoke("joy__dingli")
              room:notifySkillInvoked(p, "joy__dingli")
              room:setPlayerMark(p,"joy__dingli-round",1)
            end
          end
        end
      end
    end
  end,
})
quanxue:addEffect(fk.TurnStart, {
  name = "joy__quanxue",
  events = {fk.EventPhaseStart,fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player == target and player:hasSkill(quanxue.name) and player.phase == Player.Play
    elseif event == fk.TurnStart then
      return target:getMark("@@joy__xue") > 0
    end
  end,
  on_cost = function (self,event, target, player, data)
    if event == fk.TurnStart then return true end
    local tos = player.room:askForChoosePlayers(player,table.map(player.room:getOtherPlayers(player),
    function(p) return p.id end),1,2,"#joy__quanxue-ask",quanxue.name,true)
    if #tos > 0 then
      event:setCostData(self, {tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local tos = event:getCostData(self)[1]
      for _, pid in ipairs(tos) do
        local p = room:getPlayerById(pid)
        if not p.dead then
          room:setPlayerMark(p,"@@joy__xue",1)
        end
      end
    else
      room:setPlayerMark(target,"@@joy__xue",0)
      local choices = {"joy__quanxue_cantuse","joy__quanxue_losehp"}
      local choice = room:askForChoice(target,choices,quanxue.name,"joy__quanxue-choice")
      if choice == "joy__quanxue_losehp" and not target.dead then
        room:loseHp(target,1,quanxue.name)
      elseif choice == "joy__quanxue_cantuse" then
        room:setPlayerMark(target,"@@joy__quanxue_cantuse-turn",1)
      end
      local targets = {}
      for _, p in ipairs(player.room:getAlivePlayers()) do
        if p:hasSkill("joy__dingli") then
          if not p.dead and not target.dead and p:hasSkill("joy__dingli") and p:getMark("joy__dingli-round") == 0 then
            local n1 = p.hp
            local n2 = target.hp
            local n = math.min(n1 - n2,2)
            if n1 <= n2 then
              if room:askForSkillInvoke(p,"joy__dingli",data,"#joy__dingli-ask1") then
                player.room:recover({
                  who = p,
                  num = 1,
                  recoverBy = p,
                  skillName = "joy__quanxue"
                  })
                p:broadcastSkillInvoke("joy__dingli")
                room:notifySkillInvoked(p, "joy__dingli")
                room:setPlayerMark(p,"joy__dingli-round",1)
              end
            elseif room:askForSkillInvoke(p,"joy__dingli",data,"#joy__dingli-ask2:::"..n) then
              p:drawCards(n,"joy__dingli")
              p:broadcastSkillInvoke("joy__dingli")
              room:notifySkillInvoked(p, "joy__dingli")
              room:setPlayerMark(p,"joy__dingli-round",1)
            end
          end
        end
      end
    end
  end,
})

quanxue:addEffect("prohibit", {
  name = "#joy__quanxue_cantuse",
  is_prohibited = function(self, from, to, card)
    return from:getMark("@@joy__quanxue_cantuse-turn") > 0 and from ~= to and from.phase == Player.Play
  end,
})

return quanxue